#include "remotecontrolwidget.h"
#include "ui_remotecontrolwidget.h"
#include "commandworker.h"
#include "QThread"

RemoteControlWidget::RemoteControlWidget(MainWindow* m,QWidget *parent) :
    m_mainWindow(m),DialogBase(parent),
    ui(new Ui::RemoteControlWidget)
{
    ui->setupUi(this);
    connect(this,&RemoteControlWidget::inputCheck,m,&MainWindow::popMessageBox);
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}

RemoteControlWidget::~RemoteControlWidget()
{
    delete ui;
}

void RemoteControlWidget::on_btn_read_clicked()
{
    if(!ui->checkBox_curprotection->isChecked() && !ui->checkBox_protectiontime->isChecked())
    {
        return;
    }
    ui->btn_read->setEnabled(false);
    m_commands.clear();
    if(ui->checkBox_curprotection->isChecked())
    {
        m_commands.append(Utils::createCommandFrame(0x11,Oad1[CUR_PROTECT],0,""));
    }
    if(ui->checkBox_protectiontime->isChecked())
    {
        m_commands.append(Utils::createCommandFrame(0x11,Oad1[PROTECT_TIME],0,""));
    }

    CommandWorker* worker=new CommandWorker();
    QThread* thread=new QThread(this);

    worker->moveToThread(thread);
    connect(worker,&CommandWorker::openFailed,m_mainWindow,&MainWindow::popMessageBox);
    connect(worker,&CommandWorker::OneFrameFinished,m_mainWindow,&MainWindow::appendLog);  //连接主函数显示响应结果
    connect(worker,&CommandWorker::commandResult,this,&RemoteControlWidget::ParseData);

    connect(thread,&QThread::started,[=](){
        worker->start(m_commands);
    });
    connect(worker, &CommandWorker::allTaskFinished, worker, &QObject::deleteLater);
    connect(worker, &CommandWorker::allTaskFinished, thread, &QThread::quit);
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    thread->start();
    connect(worker,&CommandWorker::allTaskFinished,this,[=](){
        ui->btn_read->setEnabled(true);
    });
}

void RemoteControlWidget::on_btn_write_clicked()
{
    if(!ui->checkBox_curprotection->isChecked() && !ui->checkBox_protectiontime->isChecked())
    {
        return;
    }
    ui->btn_write->setEnabled(false);
    m_commands.clear();
    if(ui->checkBox_curprotection->isChecked())
    {
        if(!ui->lineEdit_curprotection->text().isEmpty())
        {
            QByteArray data;
            data.append(Utils::instance()->getPwdLevel());
            data.append(Utils::instance()->getPwd());
            data.append(QByteArray::fromHex("78563412"));
            double num=ui->lineEdit_curprotection->text().toDouble();
            data.append(Utils::reverseByteArray(Utils::doubleToBCDArray3Dot3(num)));
            m_commands.append(Utils::createCommandFrame(0x14,Oad1[CUR_PROTECT],data.size(),data));
        }

    }
    if(ui->checkBox_protectiontime->isChecked())
    {
        if(!ui->lineEdit_protectiontime->text().isEmpty())
        {
            QByteArray data;
            data.append(Utils::instance()->getPwdLevel());
            data.append(Utils::instance()->getPwd());
            data.append(QByteArray::fromHex("78563412"));
            int num=ui->lineEdit_protectiontime->text().toUInt();
            data.append(Utils::reverseByteArray(Utils::intToBcd(num,2)));
            m_commands.append(Utils::createCommandFrame(0x14,Oad1[PROTECT_TIME],data.size(),data));
        }

    }

    CommandWorker* worker=new CommandWorker();
    QThread* thread=new QThread(this);

    worker->moveToThread(thread);
    connect(worker,&CommandWorker::openFailed,m_mainWindow,&MainWindow::popMessageBox);
    connect(worker,&CommandWorker::OneFrameFinished,m_mainWindow,&MainWindow::appendLog);  //连接主函数显示响应结果
    connect(worker,&CommandWorker::commandResult,this,&RemoteControlWidget::ParseData);

    connect(thread,&QThread::started,[=](){
        worker->start(m_commands);
    });
    connect(worker, &CommandWorker::allTaskFinished, worker, &QObject::deleteLater);
    connect(worker, &CommandWorker::allTaskFinished, thread, &QThread::quit);
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    thread->start();
    connect(worker,&CommandWorker::allTaskFinished,this,[=](){
        ui->btn_write->setEnabled(true);
    });
}

void RemoteControlWidget::on_btn_set_clicked()
{
    int index=ui->comboBox_conctrl->currentIndex();
    ui->btn_set->setEnabled(false);
    m_commands.clear();
    QByteArray data;
    data.append(Utils::instance()->getPwdLevel());
    data.append(Utils::instance()->getPwd());
    data.append(QByteArray::fromHex("78563412"));
    data.append(Oad2[index]);
    QString date=ui->dateTimeEdit->dateTime().toString("ssmmhhddMMyy");
    data.append(Utils::decimalStringToBcd(date));
    m_commands.append(Utils::createCommandFrameNoDI(0x1C,data.size(),data));

    CommandWorker* worker=new CommandWorker();
    QThread* thread=new QThread(this);

    worker->moveToThread(thread);
    connect(worker,&CommandWorker::openFailed,m_mainWindow,&MainWindow::popMessageBox);
    connect(worker,&CommandWorker::OneFrameFinished,m_mainWindow,&MainWindow::appendLog);  //连接主函数显示响应结果
    connect(worker,&CommandWorker::commandResult,this,&RemoteControlWidget::ParseData);

    connect(thread,&QThread::started,[=](){
        worker->start(m_commands);
    });
    connect(worker, &CommandWorker::allTaskFinished, worker, &QObject::deleteLater);
    connect(worker, &CommandWorker::allTaskFinished, thread, &QThread::quit);
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    thread->start();
    connect(worker,&CommandWorker::allTaskFinished,this,[=](){
        ui->btn_set->setEnabled(true);
    });
}

void RemoteControlWidget::ParseData(QByteArray arr)
{
    if(arr.size()< 4+(Utils::Frame::LEN))
    {
        return;
    }
    if(arr.at(0)!=0x68)
    {
        arr.remove(0,4);
    }
    Utils::Framesub33(arr);
    unsigned char cmd=arr.at(Utils::Frame::CMD);
    switch (cmd)
    {
    case 0x91:  //正常应答
    {
        QByteArray sub=Utils::sub33(arr.mid(Utils::Frame::DI0,4));
        //sub=Utils::reverseByteArray(sub);
        if(sub==Oad1[CUR_PROTECT])
        {
            QByteArray data=Utils::reverseByteArray(arr.mid(Utils::Frame::DAT0,arr.at(Utils::Frame::LEN)-4));
            data=Utils::sub33(data);
            //QString value=QString::number(Utils::instance()->bcdToDecimalString(data,3).toDouble());
            QString value=QString::number(Utils::instance()->bcdArrayToDouble3Dot3(data));
            ui->lineEdit_curprotection->setText(value);
        }
        else if(sub==Oad1[PROTECT_TIME])
        {
            QByteArray data=Utils::reverseByteArray(arr.mid(Utils::Frame::DAT0,arr.at(Utils::Frame::LEN)-4));
            data=Utils::sub33(data);
            ui->lineEdit_protectiontime->setText(QString::number(Utils::bcdToInt(data)));
        }

    }
    case 0xb1:  //有后续数据
    {
        break;
    }
    case 0x95:
    {
        if(arr.at(Utils::Frame::LEN)==0x0)
        {
            break;
        }
        break;
    }
    }
}

void RemoteControlWidget::on_btn_setprg_clicked()
{
    int value=ui->lineEdit_prgmode->text().toUInt();
    if(value>=0 && value<=255)
    {
        ui->btn_setprg->setEnabled(false);
        m_commands.clear();
        QByteArray data;
        data.append(Utils::instance()->getPwdLevel());
        data.append(Utils::instance()->getPwd());
        data.append(QByteArray::fromHex("78563412"));
        data.append(static_cast<char>(value));
        m_commands.append(Utils::createCommandFrame(0x14,Oad_prg,data.size(),data));

        CommandWorker* worker=new CommandWorker();
        QThread* thread=new QThread(this);

        worker->moveToThread(thread);
        connect(worker,&CommandWorker::openFailed,m_mainWindow,&MainWindow::popMessageBox);
        connect(worker,&CommandWorker::OneFrameFinished,m_mainWindow,&MainWindow::appendLog);  //连接主函数显示响应结果
        connect(worker,&CommandWorker::commandResult,this,&RemoteControlWidget::ParseData);

        connect(thread,&QThread::started,[=](){
            worker->start(m_commands);
        });
        connect(worker, &CommandWorker::allTaskFinished, worker, &QObject::deleteLater);
        connect(worker, &CommandWorker::allTaskFinished, thread, &QThread::quit);
        connect(thread, &QThread::finished, thread, &QObject::deleteLater);
        thread->start();
        connect(worker,&CommandWorker::allTaskFinished,this,[=](){
            ui->btn_setprg->setEnabled(true);
        });
    }
    else
    {
        emit inputCheck("请检查输入格式，0-99");
    }
}
